package ai.people.core.security.config;

import ai.people.core.security.extractor.AuthenticatedUsername;
import ai.people.core.security.extractor.CookieAuthorizationValueExtractor;
import ai.people.core.security.extractor.HeaderAuthorizationValueExtractor;
import ai.people.core.security.extractor.TokenValueExtractor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * 安全配置
 *
 * @author yuanqinglong
 * @date 2022/5/9 18:38
 */
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    /**
     * 身份验证管理器bean
     *
     * @return {@link AuthenticationManager}
     * @throws Exception 异常
     */
    @Bean
    @Override
    @ConditionalOnMissingBean(AuthenticationManager.class)
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }



    /**
     * cookie提取token
     *
     * @return {@link TokenValueExtractor}
     */
    @Bean
    public TokenValueExtractor cookieAuthorizationValueExtractor(){
        return new CookieAuthorizationValueExtractor();
    }

    /**
     * header提取Authorization
     *
     * @return {@link TokenValueExtractor}
     */
    @Bean
    public TokenValueExtractor headerAuthorizationValueExtractor(){
        return new HeaderAuthorizationValueExtractor();
    }

    /**
     * 经过身份验证用户名
     *
     * @return {@link AuthenticatedUsername}
     */
    @Bean
    public AuthenticatedUsername authenticatedUsername(){
        return new AuthenticatedUsername();
    }
}
